package cn.dcw.pethome.pet.service.impl;

import cn.dcw.pethome.basic.exception.MyException;
import cn.dcw.pethome.basic.service.impl.BaseServiceImpl;
import cn.dcw.pethome.basic.util.LoginContext;
import cn.dcw.pethome.basic.util.PageList;
import cn.dcw.pethome.pet.domain.Pet;
import cn.dcw.pethome.pet.dto.PetDto;
import cn.dcw.pethome.pet.mapper.PetMapper;
import cn.dcw.pethome.pet.query.PetQuery;
import cn.dcw.pethome.pet.service.IPetService;
import cn.dcw.pethome.user.domain.LoginInfo;
import cn.dcw.pethome.user.domain.User;
import cn.dcw.pethome.user.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class PetServiceImpl  extends BaseServiceImpl<Pet> implements IPetService {

    @Autowired
    private PetMapper petMapper;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private UserMapper userMapper;
    /**
     * 分页查询
     * @param query
     * @return
     */
    @Override
    public PageList<Pet> getPet(PetQuery query) {

        return super.pageData(query);
    }

    /**
     * 宠物上架
     * @param ids
     */
    @Override
    public void onsale(List<Long> ids) {
        //创建一个空的map对象
        Map<String, Object> map = new HashMap<>();
        map.put("onsaleTime", new Date());//上架时间
        map.put("ids", ids);
        petMapper.onsale(map);
    }
    /**
     * 宠物下架
     * @param ids
     */
    @Override
    public void offsale(List<Long> ids) {
        //创建一个空的map对象
        Map<String, Object> map = new HashMap<>();
        map.put("offsaleTime", new Date());//下架时间
        map.put("ids", ids);
        petMapper.offSale(map);
    }

    /**
     * 批量删除
     * @param ids
     */
    @Override
    public void dels(List<Long> ids) {
        petMapper.dels(ids);
    }

    /**
     * 宠物信息添加和编辑
     * @param pet
     */
    @Override
    public void addOrUpdate(Pet pet) {
        //通过id区分编辑和添加
        if(pet.getId()==null){//添加
            pet.setSearch_master_msg_id(0L);
            petMapper.add(pet);
        }else {//编辑
            petMapper.update(pet);
        }
    }

    @Override
    public PetDto getPetById(Long id) {
        return petMapper.getPetById(id);
    }

    /**
     * 领养
     * @param id
     * @param request
     */
    @Override
    public void adopt(Long id, HttpServletRequest request) {
        //调用工具类，获取登入用户信息
        LoginInfo loginInfo = LoginContext.getLoginInfo(request);
        //如果用户登入时效过去，则给用户一个提示
        if(loginInfo==null){
            throw new MyException("请重新登入");
        }
       /* //客户点击领养后，将pet表中的static修改为0，下架  领养用户id进行关联
        Pet pet = petMapper.getPetid(id);
        pet.setState(0);
        Long loginInfoId = loginInfo.getId();
        User user = userMapper.findOneByloginInfoId(loginInfoId);
        pet.setUser_id(user.getId());
        pet.setOffsaletime(new Date());
        petMapper.update(pet);*/
    }
}
